package com.sttk.secreconeclick.bookkeep.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sttk.secreconeclick.bookkeep.BaseAc;
import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.adapter.SelTypeAdapter;
import com.sttk.secreconeclick.bookkeep.bean.AccountBookBean;
import com.sttk.secreconeclick.bookkeep.bean.BillTypeBean;
import com.sttk.secreconeclick.bookkeep.databinding.ActivitySelTypeBinding;
import com.sttk.secreconeclick.bookkeep.utils.AppSpUtils;

import java.util.ArrayList;
import java.util.List;

public class SelTypeActivity extends BaseAc<ActivitySelTypeBinding> implements View.OnClickListener, OnItemClickListener {

    public static int selPosition;
    private SelTypeAdapter selTypeAdapter;
    private AccountBookBean accountBookBean;
    private List<BillTypeBean> listSel = new ArrayList<>();
    private boolean isIncome;
    private boolean isAllSel;

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_type;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelTypeBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvExpenditure.setOnClickListener(this);
        mDataBinding.tvIncome.setOnClickListener(this);
        mDataBinding.llAllSel.setOnClickListener(this);
        mDataBinding.tvReset.setOnClickListener(this);
        mDataBinding.tvImport.setOnClickListener(this);

        mDataBinding.rvTypeList.setLayoutManager(new LinearLayoutManager(mContext));
        selTypeAdapter = new SelTypeAdapter();
        mDataBinding.rvTypeList.setAdapter(selTypeAdapter);
        selTypeAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvExpenditure) {//支出
            isIncome = false;
            mDataBinding.tvExpenditure.setBackgroundResource(R.drawable.expenditure_sel);
            mDataBinding.tvIncome.setBackgroundResource(0);
            mDataBinding.ivAllSel.setImageResource(R.drawable.noselect2);
            isAllSel = false;
            listSel.clear();
            for (BillTypeBean billTypeBean : accountBookBean.getIncomeTypeList()) {
                billTypeBean.setSelected(false);
            }
            setData();
        } else if (id == R.id.tvIncome) {//收入
            isIncome = true;
            mDataBinding.tvExpenditure.setBackgroundResource(0);
            mDataBinding.tvIncome.setBackgroundResource(R.drawable.income_sel);
            mDataBinding.ivAllSel.setImageResource(R.drawable.noselect2);
            isAllSel = false;
            listSel.clear();
            for (BillTypeBean billTypeBean : accountBookBean.getExpenditureTypeList()) {
                billTypeBean.setSelected(false);
            }
            setData();
        } else if (id == R.id.llAllSel) {//全选
            listSel.clear();
            if (isAllSel) {
                isAllSel = false;
                if (isIncome) {
                    for (BillTypeBean billTypeBean : accountBookBean.getIncomeTypeList()) {
                        billTypeBean.setSelected(false);
                    }
                } else {
                    for (BillTypeBean billTypeBean : accountBookBean.getExpenditureTypeList()) {
                        billTypeBean.setSelected(false);
                    }
                }
                setData();
            } else {
                isAllSel = true;
                if (isIncome) {
                    for (BillTypeBean billTypeBean : accountBookBean.getIncomeTypeList()) {
                        billTypeBean.setSelected(true);
                        listSel.add(billTypeBean);
                    }
                } else {
                    for (BillTypeBean billTypeBean : accountBookBean.getExpenditureTypeList()) {
                        billTypeBean.setSelected(true);
                        listSel.add(billTypeBean);
                    }
                }
                setData();
            }
            mDataBinding.ivAllSel.setImageResource(isAllSel ? R.drawable.select3 : R.drawable.noselect2);
        } else if (id == R.id.tvReset) {//重置
            isAllSel = false;
            listSel.clear();
            if (isIncome) {
                for (BillTypeBean billTypeBean : accountBookBean.getIncomeTypeList()) {
                    billTypeBean.setSelected(false);
                }
            } else {
                for (BillTypeBean billTypeBean : accountBookBean.getExpenditureTypeList()) {
                    billTypeBean.setSelected(false);
                }
            }
            mDataBinding.ivAllSel.setImageResource(isAllSel ? R.drawable.select3 : R.drawable.noselect2);
            setData();
        } else if (id == R.id.tvImport) {//导入
            if (listSel.size() == 0) return;
            List<AccountBookBean> list = AppSpUtils.getAccountBookList();
            AccountBookBean bean = list.get(ManageTypeActivity.accountBookPos);
            if (isIncome) {
                bean.getIncomeTypeList().addAll(listSel);
            } else {
                bean.getExpenditureTypeList().addAll(listSel);
            }
            AppSpUtils.saveAccountBookList(list);
            ToastUtils.showShort(getString(R.string.import_suc));
            finish();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (selTypeAdapter.getItem(position).isSelected()) {
            selTypeAdapter.getItem(position).setSelected(false);
            listSel.remove(selTypeAdapter.getItem(position));
        } else {
            listSel.add(selTypeAdapter.getItem(position));
            selTypeAdapter.getItem(position).setSelected(true);
        }
        selTypeAdapter.notifyDataSetChanged();
        if (listSel.size() == selTypeAdapter.getItemCount()) {
            isAllSel = true;
            mDataBinding.ivAllSel.setImageResource(R.drawable.select3);
        } else {
            isAllSel = false;
            mDataBinding.ivAllSel.setImageResource(R.drawable.noselect2);
        }
    }

    @Override
    protected void initData() {
        accountBookBean = AppSpUtils.getAccountBookList().get(selPosition);
        setData();
    }

    private void setData() {
        if (isIncome) {
            if (accountBookBean.getIncomeTypeList() != null && accountBookBean.getIncomeTypeList().size() > 0) {
                selTypeAdapter.setList(accountBookBean.getIncomeTypeList());
                mDataBinding.rvTypeList.setVisibility(View.VISIBLE);
                mDataBinding.tvTypeNoData.setVisibility(View.GONE);
            } else {
                mDataBinding.rvTypeList.setVisibility(View.GONE);
                mDataBinding.tvTypeNoData.setVisibility(View.VISIBLE);
            }
        } else {
            if (accountBookBean.getExpenditureTypeList() != null && accountBookBean.getExpenditureTypeList().size() > 0) {
                selTypeAdapter.setList(accountBookBean.getExpenditureTypeList());
                mDataBinding.rvTypeList.setVisibility(View.VISIBLE);
                mDataBinding.tvTypeNoData.setVisibility(View.GONE);
            } else {
                mDataBinding.rvTypeList.setVisibility(View.GONE);
                mDataBinding.tvTypeNoData.setVisibility(View.VISIBLE);
            }
            selTypeAdapter.setList(accountBookBean.getExpenditureTypeList());
        }
    }
}